import { json } from "@sveltejs/kit";
import usersModule from "$lib/modules/users";
import { hashPassword } from "$lib/utils/password";

export async function POST({ request }) {
  const { username, password : plainPassword } = await request.json();
  if (!username || !plainPassword) { return json({ message: "用户名和密码不能为空", status: false }, { status: 400 }); }
  const existingUser = await usersModule.getByUsername(username);
  if (existingUser) { return json({ message: "用户名已存在", status: false }, { status: 400 }); }
  const password = await hashPassword(plainPassword);
  const user = await usersModule.createUser({ username, password });
  if (!user) { return json({ message: "注册失败，请稍后重试", status: false }, { status: 500 }); }
  return json({ message: "注册成功", status: true });
}
